//
//  RootTableViewController.m
//  KKThread
//
//  Created by Mac mini on 16/2/15.
//  Copyright © 2016年 JuniorCodingPerson. All rights reserved.
//

#import "RootTableViewController.h"

@interface RootTableViewController ()

@property (nonatomic, strong) NSArray *arr_gcdItems;

@end

@implementation RootTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.arr_gcdItems = @[@"串行队列，同步执行",@"串行队列，异步执行",@"并行队列，同步执行",@"并行队列，异步执行",@"异步线程组——notify",@"异步线程组——wait",@"NSOperationQueue"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arr_gcdItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"RootCell" forIndexPath:indexPath];
    
    cell.textLabel.text = self.arr_gcdItems[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.row) {
        case 0:
            [self private_serial_sync];
            break;
        case 1:
            [self private_serial_async];
            break;
        case 2:
            [self private_concurrent_sync];
            break;
        case 3:
            [self private_concurrent_async];
            break;
        case 4:
            [self private_groupNotify];
            break;
        case 5:
            [self private_groupWait];
            break;
        case 6:
            [self private_queue];
            break;
        default:
            break;
    }
}

- (void)private_serial_sync{
    //  串行队列，同步执行
    NSLog(@"串行队列，同步执行");
    NSLog(@"syncConcurrent---begin");
    NSLog(@"......%@",[NSThread currentThread]);
    
    dispatch_queue_t queue = dispatch_queue_create("KK.KKThread.com", DISPATCH_QUEUE_SERIAL);
    dispatch_sync(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"任务1......%@",[NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"任务2......%@",[NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"任务3......%@",[NSThread currentThread]);
    });
    NSLog(@"syncConcurrent---end");
}

- (void)private_serial_async{
    //  串行队列，同步执行
    NSLog(@"串行队列，异步执行");
    NSLog(@"syncConcurrent---begin");
    NSLog(@"......%@",[NSThread currentThread]);
    
    dispatch_queue_t queue = dispatch_queue_create("KK.KKThread.com", DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"任务1......%@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"任务2......%@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"任务3......%@",[NSThread currentThread]);
    });
    NSLog(@"syncConcurrent---end");
}

- (void)private_concurrent_sync{
    //  串行队列，同步执行
    NSLog(@"并行队列，异步执行");
    NSLog(@"syncConcurrent---begin");
    NSLog(@"......%@",[NSThread currentThread]);
    
    dispatch_queue_t queue = dispatch_queue_create("KK.KKThread.com", DISPATCH_QUEUE_CONCURRENT);
    dispatch_sync(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"任务1......%@",[NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"任务2......%@",[NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"任务3......%@",[NSThread currentThread]);
    });
    NSLog(@"syncConcurrent---end");
}

- (void)private_concurrent_async{
    //  串行队列，同步执行
    NSLog(@"并行队列，异步执行");
    NSLog(@"syncConcurrent---begin");
    NSLog(@"......%@",[NSThread currentThread]);
    
    dispatch_queue_t queue = dispatch_queue_create("KK.KKThread.com", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"任务1......%@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"任务2......%@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"任务3......%@",[NSThread currentThread]);
    });
    NSLog(@"syncConcurrent---end");
}

- (void)private_groupNotify {
    NSLog(@"currentThread---%@",[NSThread currentThread]);  // 打印当前线程
    NSLog(@"group---begin");
    
    dispatch_group_t group =  dispatch_group_create();
    
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 追加任务1
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];              // 模拟耗时操作
            NSLog(@"1---%@",[NSThread currentThread]);      // 打印当前线程
        }
    });
    
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 追加任务2
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];              // 模拟耗时操作
            NSLog(@"2---%@",[NSThread currentThread]);      // 打印当前线程
        }
    });
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        // 等前面的异步任务1、任务2都执行完毕后，回到主线程执行下边任务
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];              // 模拟耗时操作
            NSLog(@"3---%@",[NSThread currentThread]);      // 打印当前线程
        }
        NSLog(@"group---end");
    });
}

- (void)private_groupWait {
    NSLog(@"currentThread---%@",[NSThread currentThread]);  // 打印当前线程
    NSLog(@"group---begin");
    
    dispatch_group_t group =  dispatch_group_create();
    
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 追加任务1
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];              // 模拟耗时操作
            NSLog(@"1---%@",[NSThread currentThread]);      // 打印当前线程
        }
    });
    
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 追加任务2
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];              // 模拟耗时操作
            NSLog(@"2---%@",[NSThread currentThread]);      // 打印当前线程
        }
    });
    
    // 等待上面的任务全部完成后，会往下继续执行（会阻塞当前线程）
    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    
    NSLog(@"group---end");
}

- (void)private_queue {
    
    NSLog(@"开始.....%@",[NSThread currentThread]);
    
    NSOperationQueue *queue = [NSOperationQueue new];
    queue.maxConcurrentOperationCount = 1;
    NSLog(@"创建了线程队列.....%@",queue);
    
    NSBlockOperation *option1 = [NSBlockOperation blockOperationWithBlock:^{
        [NSThread sleepForTimeInterval:3.0];
        NSLog(@"任务1.....%@",[NSThread currentThread]);
    }];
    [queue addOperation:option1];
    
    NSBlockOperation *option2 = [NSBlockOperation blockOperationWithBlock:^{
        [NSThread sleepForTimeInterval:3.0];
        NSLog(@"任务2.....%@",[NSThread currentThread]);
    }];
    [queue addOperation:option2];
    
    NSBlockOperation *option3 = [NSBlockOperation blockOperationWithBlock:^{
        [NSThread sleepForTimeInterval:3.0];
        NSLog(@"任务3.....%@",[NSThread currentThread]);
    }];
    [queue addOperation:option3];
    
    NSBlockOperation *option4 = [NSBlockOperation blockOperationWithBlock:^{
        [NSThread sleepForTimeInterval:3.0];
        NSLog(@"任务4.....%@",[NSThread currentThread]);
    }];
    [queue addOperation:option4];
    
    NSBlockOperation *option5 = [NSBlockOperation blockOperationWithBlock:^{
        [NSThread sleepForTimeInterval:3.0];
        NSLog(@"任务5.....%@",[NSThread currentThread]);
    }];
    [queue addOperation:option5];
    
    NSBlockOperation *option6 = [NSBlockOperation blockOperationWithBlock:^{
        [NSThread sleepForTimeInterval:3.0];
        NSLog(@"任务6.....%@",[NSThread currentThread]);
    }];
    [queue addOperation:option6];
    
    NSBlockOperation *option7 = [NSBlockOperation blockOperationWithBlock:^{
        [NSThread sleepForTimeInterval:3.0];
        NSLog(@"任务7.....%@",[NSThread currentThread]);
    }];
    [queue addOperation:option7];
    
    NSBlockOperation *option8 = [NSBlockOperation blockOperationWithBlock:^{
        [NSThread sleepForTimeInterval:3.0];
        NSLog(@"任务8.....%@",[NSThread currentThread]);
    }];
    [queue addOperation:option8];
    
    NSBlockOperation *option9 = [NSBlockOperation blockOperationWithBlock:^{
        [NSThread sleepForTimeInterval:3.0];
        NSLog(@"任务9.....%@",[NSThread currentThread]);
    }];
    [queue addOperation:option9];
    
    NSBlockOperation *option10 = [NSBlockOperation blockOperationWithBlock:^{
        [NSThread sleepForTimeInterval:3.0];
        NSLog(@"任务10.....%@",[NSThread currentThread]);
    }];
    [queue addOperation:option10];
    
    NSBlockOperation *option11 = [NSBlockOperation blockOperationWithBlock:^{
        [NSThread sleepForTimeInterval:3.0];
        NSLog(@"任务11.....%@",[NSThread currentThread]);
    }];
    [queue addOperation:option11];
}

@end
